﻿#include"contact.h"
void initcontact(struct contact* cons)
{
	cons->date = (struct people*)malloc(SIZE * sizeof(struct people));
	if (cons->date == NULL)
	{
		printf("错误！\n");
		return;
	}
	cons->num = 0;
	cons->malloc = SIZE;
}
void addcontact(struct contact* cons)
{
	if (cons->num == cons->malloc)
	{
		struct people* ptr = (struct people*)realloc(cons->date, (cons->malloc + MALLOC) * sizeof(struct people));
		if (ptr != NULL)
		{
			cons->date = ptr;
			cons->malloc += MALLOC;
		}
		else
		{
			printf("错误！\n");
			return;
		}
	}
	printf("请输入要添加的姓名>:");
	scanf("%s", cons->date[cons->num].name);
	printf("请输入要添加的性别>:");
	scanf("%s", cons->date[cons->num].sex);
	printf("请输入要添加的年龄>:");
	scanf("%d", &(cons->date[cons->num].age));
	printf("请输入要添加的电话>:");
	scanf("%s", cons->date[cons->num].tele);
	printf("请输入要添加的地址>:");
	scanf("%s", cons->date[cons->num].address);
	printf("成功添加联系人！\n");
	cons->num++;
	return;
}
int findname(struct contact* cons, char name[NAME])
{
	for (int i = 0; i < cons->num; i++)
	{
		if (0 == strcmp(name, cons->date[i].name))
		{
			return i;
		}

	}
	return -1;
}
void delecontact(struct contact* cons)
{
	char name[NAME];
	printf("请输入要删除联系人的姓名>:");
	scanf("%s", name);
	int ret = findname(cons, name);
	if (-1 == ret)
	{
		printf("找不到该联系人！\n");
	}
	else
	{
		for (int j = ret; j < cons->num - 1; j++)
		{
			cons->date[j] = cons->date[j + 1];
		}
		printf("成功删除该联系人！\n");
		cons->num--;
	}
}
void freerealloc(struct contact* cons)
{
	free(cons->date);
	cons->date = NULL;
	cons->malloc = 0;
	cons->num = 0;
}
void selectcontact(struct contact* cons)
{
	char name[NAME];
	printf("请输入要查询联系人的姓名>:");
	scanf("%s", name);
	int ret = findname(cons, name);
	if (-1 == ret)
	{
		printf("找不到该联系人！\n");
	}
	else
	{
		printf("%-20s\t%-6s\t%-4s\t%-12s\t%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
		printf("%-20s\t%-6s\t%-4d\t%-12s\t%-30s\n", cons->date[ret].name, cons->date[ret].sex, cons->date[ret].age, cons->date[ret].tele, cons->date[ret].address);
	}

}
void modifycontact(struct contact* cons)
{

	char name[NAME];
	printf("请输入要修改联系人的姓名>:");
	scanf("%s", name);
	int ret = findname(cons, name);
	if (-1 == ret)
	{
		printf("找不到该联系人！\n");
	}
	else
	{
		printf("请输入要修改的姓名>:");
		scanf("%s", cons->date[ret].name);
		printf("请输入要修改的性别>:");
		scanf("%s", cons->date[ret].sex);
		printf("请输入要修改的年龄>:");
		scanf("%d", &(cons->date[ret].age));
		printf("请输入要修改的电话>:");
		scanf("%s", cons->date[ret].tele);
		printf("请输入要修改的地址>:");
		scanf("%s", cons->date[ret].address);
		printf("成功修改联系人！\n");

	}
}
void showcontact(struct contact* cons)
{
	printf("%-20s\t%-6s\t%-4s\t%-12s\t%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < cons->num; i++)
	{
		printf("%-20s\t%-6s\t%-4d\t%-12s\t%-30s\n", cons->date[i].name, cons->date[i].sex, cons->date[i].age, cons->date[i].tele, cons->date[i].address);
	}
}
int cmp(void* p1, void* p2)
{
	return ((struct people*)p1)->age - ((struct people*)p2)->age;
}
void sortcontact(struct contact* cons)
{
	qsort(cons->date, cons->num, sizeof(struct people), cmp);
	printf("排序完成!\n");
}